package com.atguigu.team.service;

import com.atguigu.team.domain.Architect;
import com.atguigu.team.domain.Designer;
import com.atguigu.team.domain.Employee;
import com.atguigu.team.domain.Programmer;

public class TeamService {
    private static int counter=1;//给memberId赋值使用
    private final int  MAX_MEMBER=5; //限制开发团队人数
    private Programmer[] team=new Programmer[MAX_MEMBER];
    private int total;//记录开发团队的实际人数
    public Programmer[] getTeam(){
        Programmer[] res=new Programmer[total];
        for(int i=0;i<total;i++){
            res[i]=team[i];
        }
        return res;
    }
    public void addMember(Employee e)throws TeamException{
        //查看team中的程序员，架构师，设计师数量
        int numOfProgrammer=0,numOfDesigner=0,numOfArchitect=0;
        for(int i=0;i<total;i++){
            if(team[i] instanceof Architect){
                numOfArchitect++;
            }else if(team[i] instanceof Designer){
                numOfDesigner++;
            }else{
                numOfProgrammer++;
            }
        }
        if(total>=MAX_MEMBER){
            throw new TeamException("成员已满，无法添加");
        }
        if(!(e instanceof Programmer)) {
            throw new TeamException("该成员不是开发人员，无法添加");
        }
        if(isExist(e)){
            throw new TeamException("该员工已在开发团队中");
        }
        Programmer p=(Programmer) e;
        if(p.getStatus().getNAME().equals("BUSY")){
            throw new TeamException(("该员工在其他团队中"));
        }else if("VOCATION".equals(p.getStatus().getNAME())){
            throw new TeamException("该员工正在度假");
        }
        //最多一名架构师
        if(p instanceof Architect){
            if(numOfArchitect>=1){
                throw new TeamException("架构师人数已满");
            }
        }else if(p instanceof Designer){
            if(numOfDesigner>=2){
                throw new TeamException("设计师人数已满");
            }else{
                if(numOfProgrammer>=3){
                    throw new TeamException("程序员人数已满");
                }
            }
            p.setStatus(Status.BUSY);
            p.setMemberId(counter++);
            team[total++]=p;
        }
    }
    public void removeMember(int memberId)throws TeamException{
        int i=0;
        for(;i<total;i++){
            if(team[i].getMemberId()==memberId){
                team[i].setStatus(Status.FREE);
                break;
            }
        }
        if(i==total){
            throw new TeamException("找不到指定memberId员工，无法删除");
        }
        for(int j=i+1;j<total;j++){
            team[j-1]=team[j];
        }
        team[total-1]=null;
        total--;
    }
    //判断指定员工是否在开发团队之中
    public boolean isExist(Employee employee){
        for(int i=0;i<total;i++){
            if(employee.getId()==team[i].getId()){
                return true;
            }
        }
        return false;
    }
}
